package com.jio.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jio.entity.Role;
import com.jio.entity.User;
import com.jio.entity.Video;
import com.jio.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    VideoService videoService;
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page, limit);
        User user= (User) request.getSession().getAttribute("user");
        List<Video> all = videoService.findAll(user.getId());
        PageInfo<Video> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/video/video-list";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        //获取上传文件的原名
        String name=file.getOriginalFilename();
        //使用UUID生成不重复字符串进行文件名称的替换
        name= UUID.randomUUID().toString()+name.substring(name.lastIndexOf("."));
        file.transferTo(new File("d://upload",name));
        //这里返回json格式数据 是因为页面LayUI框架需求格式 返回的url为需要存入数据库的信息
        return "{\"code\":1,\"url\":\""+name+"\"}";
    }

    @RequestMapping("/add")
    public String add(HttpSession session, Video video) throws IOException {
        User user= (User) session.getAttribute("user");
        video.setTid(user.getId());
        videoService.add(video);
        return "redirect:/video/findAll";
    }

    @RequestMapping("/deleteById")
    public String deleteById(int id){
        videoService.delete(id);
        return "redirect:/video/findAll";
    }

    @RequestMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody List<Integer> ids){
        videoService.deleteList(ids);
        return "/video/findAll";
    }

    @RequestMapping("/show")
    public String show(HttpServletRequest request,int id){
        Video byId = videoService.findById(id);
        request.setAttribute("video",byId);
        return "video/video-show";
    }

    @RequestMapping("findById")
    public String findById(HttpServletRequest request,int id){
        Video byId = videoService.findById(id);
        request.setAttribute("video",byId);
        return "video/video-edit";
    }
    //视频修改
    @RequestMapping("edit")
    public String edit(Video video){
        videoService.edit(video);
        return "redirect:/video/findAll";
    }


}
